﻿using System;
using System.Windows.Forms;
using UniMana.Data;
using UniMana.Utils.Data;
using System.Linq;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System.Data;

namespace UniMana.Win.Training
{
    public partial class FrmStudent_Class_Edit : DevExpress.XtraEditors.XtraForm
    {
        private BindingSource m_bdsClass = new BindingSource();
        DataTable m_tblTable;
        private DBMain m_data = new DBMain();
        private int _Flag = 1;

        private Personnel_PersonnelInfo m_Personnel = new Personnel_PersonnelInfo();
        private Training_ClassInfo clsinfo;

        private int m_iDepartmentKey = 0;
        private int m_iMajorKey = 0;
        private int m_iLevelKey = 0;
        private int m_iCourseKey = 0;
        private int m_iTrainingTypeKey = 0;        

        private int m_iClassKey = 0;
        private string m_strClassID;

        /// <summary>
        /// Khởi tạo
        /// </summary>
        /// <param name="m_iDepartmentKey"></param>
        /// <param name="m_iMajorKey"></param>
        /// <param name="m_iTrainingTypeKey"></param>
        /// <param name="m_iCourseKey"></param>
        /// <param name="m_iLevelKey"></param>
        /// <param name="m_iClassKey"></param>
        /// <param name="m_strID"></param>
        /// <param name="m_strName"></param>

        public FrmStudent_Class_Edit(int m_iClassKey)
        {
            InitializeComponent();
            this.m_iClassKey = m_iClassKey;
        }

        /// <summary>
        /// Load dữ liệu lên form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void FrmStudent_Class_Edit_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            clsinfo = new Training_ClassInfo(m_iClassKey);
            txtID.Text = clsinfo.Properties.ID;
            m_strClassID = clsinfo.Properties.ID;
            txtName.Text = clsinfo.Properties.Name;

            m_tblTable = new DataTable();
            m_tblTable = m_data.m_DCReadOnly.Personnel_Personnels.GetPersonnelKeyIDLastNameName().ToDataTable();
            if (m_tblTable.Rows.Count != 0)
                gluPersonnel_Personnel.GetData(m_tblTable, "Personnel_PersonnelKey", "FullName");
            try
            {
                gluPersonnel_Personnel.EditValue = Guid.Parse(clsinfo.Properties.Personnel_PersonnelKey.ToString());
            }
            catch { gluPersonnel_Personnel.EditValue = null; }

            lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments.Where(tb => tb.StudentManage == true));
            lueCatalogue_Level.GetData(m_data.m_DCReadOnly.Catalogue_Levels);
            gluCatalogue_Major.GetData(m_data.m_DCReadOnly.Catalogue_Majors.GetMajorLevel(m_data.m_DCReadOnly.Catalogue_Levels, m_iDepartmentKey), "Catalogue_MajorKey", "Name");
            lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable());
            lueCatalogue_Course.GetData(m_data.m_DCReadOnly.Catalogue_Courses);

            try { m_iDepartmentKey = int.Parse(clsinfo.Properties.Catalogue_DepartmentKey.ToString()); }
            catch { m_iDepartmentKey = 0; }
            try { m_iMajorKey = int.Parse(clsinfo.Properties.Catalogue_MajorKey.ToString()); }
            catch { m_iMajorKey = 0; }
            try { m_iLevelKey = int.Parse(clsinfo.Properties.Catalogue_LevelKey.ToString()); }
            catch { m_iLevelKey = 0; }
            try { m_iCourseKey = int.Parse(clsinfo.Properties.Catalogue_CourseKey.ToString()); }
            catch { m_iCourseKey = 0; }
            try { m_iTrainingTypeKey = int.Parse(clsinfo.Properties.Catalogue_TrainingType.ToString()); }
            catch { m_iTrainingTypeKey = 0; }

            if (m_iDepartmentKey > 0) lueCatalogue_Department.EditValue = m_iDepartmentKey;
            if (m_iMajorKey > 0) gluCatalogue_Major.EditValue = m_iMajorKey;
            if (m_iLevelKey > 0) lueCatalogue_Level.EditValue = m_iLevelKey;
            if (m_iCourseKey > 0) lueCatalogue_Course.EditValue = m_iCourseKey;
            if (m_iTrainingTypeKey > 0) lueCatalogue_TrainingType.EditValue = m_iTrainingTypeKey;

            LoadClass();
        }

        /// <summary>
        /// Lấy thông tin Khoa, ngành, bậc đào tạo, khóa, loại hình đào tạo
        /// </summary>

        private void GetData()
        {
            try { m_iDepartmentKey = int.Parse(lueCatalogue_Department.EditValue.ToString()); }
            catch { m_iDepartmentKey = 0; }
            try { m_iMajorKey = int.Parse(gluCatalogue_Major.EditValue.ToString()); }
            catch { m_iMajorKey = 0; }
            try { m_iLevelKey = int.Parse(lueCatalogue_Level.EditValue.ToString()); }
            catch { m_iLevelKey = 0; }
            try { m_iCourseKey = int.Parse(lueCatalogue_Course.EditValue.ToString()); }
            catch { m_iCourseKey = 0; }
            try { m_iTrainingTypeKey = int.Parse(lueCatalogue_TrainingType.EditValue.ToString()); }
            catch { m_iTrainingTypeKey = 0; }
        }

        /// <summary>
        /// Kiểm tra đủ thông tin Khoa, ngành, bậc đào tạo, khóa, loại hình đào tạo
        /// </summary>
        /// <returns></returns>

        private bool CheckInfo()
        {
            if ((m_iDepartmentKey <= 0) || (m_iMajorKey <= 0) || (m_iLevelKey <= 0) || (m_iCourseKey <= 0) || (m_iTrainingTypeKey <= 0))
                return false;
            else
                return true;
        }

        /// <summary>
        /// Kiểm tra mã lớp vừa chỉnh sửa
        /// </summary>
        /// <returns></returns>

        private bool CheckToEdit(string _ID)
        {
            if (m_data.m_DCReadOnly.Training_Classes.CheckIDClassIdentical(_ID))
                return false;
            else
                return true;
        }

        /// <summary>
        /// Lấy danh sách lớp
        /// </summary>

        private void LoadClass()
        {
            m_bdsClass.DataSource = this.m_data.m_DCReadOnly.Training_Classes.GetClassByDepartmentMajorTraningTypeCourseLevel(this.m_data.m_DCReadOnly.Personnel_Personnels,
                m_iDepartmentKey,
                m_iMajorKey,
                m_iTrainingTypeKey,
                m_iCourseKey, m_iLevelKey);
            if (m_bdsClass.Count != 0)
                gctClass.DataSource = m_bdsClass;
            else
                gctClass.DataSource = m_bdsClass;
            grbClass.Text = "Danh sách lớp hiện tại có " + m_bdsClass.Count + " lớp.";
        }

        private void Finish()
        {
            XtraMessageBox.Show("Đã lưu","Thông báo");
            LoadClass();
        }

        /// <summary>
        /// Chỉnh sửa lớp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnEdit_Click(object sender, EventArgs e)
        {
            GetData();
            if ((txtID.Text != "") && (txtName.Text != ""))
            {
                Training_ClassInfo stdClass = new Training_ClassInfo(m_iClassKey);
                // Cập nhật cả mã lớp
                if (txtID.Text != m_strClassID)
                {
                    if (CheckToEdit(txtID.Text))
                    {
                        stdClass.Properties.ID = txtID.Text;
                        stdClass.Properties.Name = txtName.Text;
                        try { stdClass.Properties.Personnel_PersonnelKey = Guid.Parse(gluPersonnel_Personnel.EditValue.ToString()); }
                        catch { }
                        if (CheckInfo() == false)
                        {
                            DialogResult result = XtraMessageBox.Show("Không có đủ thông tin Khoa, bậc đào tạo, ngành, loại hình đào tạo, khóa học" + "\n" + "\t" + " - Chọn YES để cập nhật mã lớp, tên lớp hoặc giảng viên nếu có" + "\n" + "\t" + " - Chọn NO để quay lại", "Thông báo", MessageBoxButtons.YesNo);
                            if (result == DialogResult.Yes)
                            {
                                stdClass.Save();
                                Finish();
                                return;
                            }
                            else return;
                        }
                        else
                        {
                            stdClass.Properties.Catalogue_DepartmentKey = m_iDepartmentKey;
                            stdClass.Properties.Catalogue_MajorKey = m_iMajorKey;
                            stdClass.Properties.Catalogue_LevelKey = m_iLevelKey;
                            stdClass.Properties.Catalogue_CourseKey = m_iCourseKey;
                            stdClass.Properties.Catalogue_TrainingType = m_iTrainingTypeKey;
                            stdClass.Save();
                            Finish();
                            return;
                        }
                    }
                    else XtraMessageBox.Show("Mã lớp đã tồn tại", "Thông báo");
                }
                // Không cập nhật mã lớp
                else
                {
                    stdClass.Properties.ID = txtID.Text;
                    stdClass.Properties.Name = txtName.Text;
                    try { stdClass.Properties.Personnel_PersonnelKey = Guid.Parse(gluPersonnel_Personnel.EditValue.ToString()); }
                    catch { }
                    if (CheckInfo() == false)
                    {
                        DialogResult result = XtraMessageBox.Show("Không có đủ thông tin Khoa, bậc đào tạo, ngành, loại hình đào tạo, khóa học" + "\n" + "\t" + " - Chọn YES để cập nhật mã lớp, tên lớp hoặc giảng viên nếu có" + "\n" + "\t" + " - Chọn NO để quay lại", "Thông báo", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            stdClass.Save();
                            Finish();
                            return;
                        }
                        else return;
                    }
                    else
                    {
                        stdClass.Properties.Catalogue_DepartmentKey = m_iDepartmentKey;
                        stdClass.Properties.Catalogue_MajorKey = m_iMajorKey;
                        stdClass.Properties.Catalogue_LevelKey = m_iLevelKey;
                        stdClass.Properties.Catalogue_CourseKey = m_iCourseKey;
                        stdClass.Properties.Catalogue_TrainingType = m_iTrainingTypeKey;
                        stdClass.Save();
                        Finish();
                        return;
                    }
                }
            }
            else
                XtraMessageBox.Show("Thông tin mã lớp và tên lớp không đầy đủ", "Thông báo");
        }

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (_Flag == 1)
                {
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) <= 0)
                    {
                        // Nạp lại dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học

                        lueCatalogue_Level.EditValue = null;
                        lueCatalogue_Level.GetData((this.m_data.m_DCReadOnly.Catalogue_Levels.GetLevelInTrainingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);

                        gluCatalogue_Major.EditValue = null;
                        gluCatalogue_Major.GetData((this.m_data.m_DCReadOnly.Catalogue_Majors.GetMajorInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, m_data.m_DCReadOnly.Catalogue_Levels)).ToDataTable(), "Catalogue_MajorKey", noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);

                        lueCatalogue_Course.EditValue = null;
                        lueCatalogue_Course.GetData((this.m_data.m_DCReadOnly.Catalogue_Courses.GetCourseKeyNameInTrainingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);
                    }
                    else
                    {
                        // Lấy dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học theo khoa

                        lueCatalogue_Level.EditValue = null;
                        this.lueCatalogue_Level.GetData((this.m_data.m_DCReadOnly.Catalogue_Levels.GetLevelByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);

                        gluCatalogue_Major.EditValue = null;
                        this.gluCatalogue_Major.GetData((this.m_data.m_DCReadOnly.Catalogue_Majors.GetMajorByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, m_data.m_DCReadOnly.Catalogue_Levels, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), "Catalogue_MajorKey", noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        this.lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);

                        lueCatalogue_Course.EditValue = null;
                        this.lueCatalogue_Course.GetData((this.m_data.m_DCReadOnly.Catalogue_Courses.GetCourseByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);
                    }
                }
            }
            catch { }
        }

        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue) > 0)
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) > 0)
                        gluCatalogue_Major.EditValue = (this.m_data.m_DCReadOnly.Catalogue_Majors.GetMajorKeyByDepartmentLevel(this.m_data.m_DCReadOnly.Training_TrainingPrograms, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue), GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue))).ToDataTable().Rows[0]["Catalogue_MajorKey"];
            }
            catch { }
        }

        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue) > 0)
                {
                    _Flag = 0;
                    lueCatalogue_Department.EditValue = (this.m_data.m_DCReadOnly.Catalogue_Majors.GetDepartmentKeyByMajor(this.m_data.m_DCReadOnly.Training_TrainingPrograms, GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue))).ToDataTable().Rows[0]["Catalogue_DepartmentKey"];
                    _Flag = 1;
                    lueCatalogue_Level.EditValue = (this.m_data.m_DCReadOnly.Catalogue_Majors.GetLevelKeyByDepartmentMajor(this.m_data.m_DCReadOnly.Training_TrainingPrograms, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue), GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue))).ToDataTable().Rows[0]["Catalogue_LevelKey"];
                }
            }
            catch { }
        }

        private void gctClass_Click(object sender, EventArgs e)
        {
            m_iClassKey = int.Parse(grvClass.GetFocusedRowCellValue("Training_ClassKey").ToString());
            LoadData();
        }
    }
}
